package com.zsc.edu.gateway.modules.iot.tsl.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zsc.edu.gateway.modules.iot.tsl.dto.PropertyDto;
import com.zsc.edu.gateway.modules.iot.tsl.entity.Property;
import com.zsc.edu.gateway.modules.iot.tsl.query.PropertyQuery;
import com.zsc.edu.gateway.modules.iot.tsl.service.PropertyService;
import com.zsc.edu.gateway.modules.operationLog.entity.OperationLogAnnotation;
import lombok.AllArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author Yao
 */
@AllArgsConstructor
@RestController
@RequestMapping("api/rest/tsl/property")
public class PropertyController {

    private final PropertyService service;


    /**
     * 创建属性
     *
     * @param dto 创建的属性
     * @return
     */
    @PostMapping
    @PreAuthorize("hasAuthority('iot:property:create')")
    @OperationLogAnnotation(content = "'属性'", operationType = "新建")
    public Property create(@RequestBody PropertyDto dto) {
        return service.create(dto);
    }


    /**
     * 更新属性
     *
     * @return 更新后的属性
     */
    @PatchMapping("{id}")
    @PreAuthorize("hasAuthority('iot:property:update')")
    @OperationLogAnnotation(content = "'属性'", operationType = "更新")
    public Property update(@RequestBody PropertyDto dto, @PathVariable("id") Long id) {
        return service.update(dto, id);
    }


    /**
     * 查询属性
     *
     * @param query    属性的查询表单
     * @param page 分页参数
     * @return Page<Device> 属性分页数据
     */
    @GetMapping
    @PreAuthorize("hasAuthority('iot:property:query')")
    public Page<Property> query(PropertyQuery query, Page<Property> page) {
        return service.page(page, query.wrapper());
    }
    /**
     * 删除属性
     *
     * @param id 属性编号
     * @return 被删除的属性
     */
    @DeleteMapping("{id}")
    @PreAuthorize("hasAuthority('iot:property:delete')")
    @OperationLogAnnotation(content = "'属性'", operationType = "删除")
    public Boolean delete(@PathVariable("id") Long id) {
        return service.removeById(id);
    }

    /**
     * 属性
     * 查询详情
     *
     * @param id ID
     * @return 任务
     */
    @GetMapping("{id}")
    @PreAuthorize("hasAuthority('iot:property:query')")
    public Property detail(@PathVariable("id") Long id) {
        return service.detail(id);
    }
}
